public class Solution {
    public static int maxScore(int[] cardPoints, int k) {
        int ret = 0;
        for(int x:cardPoints) ret += x;
        //固定滑动窗口  求窗口中最小元素和
        int n = cardPoints.length;
        int len = n - k;  //窗口长度
        if(len == 0) return ret;

        int sum = Integer.MAX_VALUE;
        int tmp = 0;  //临时存储元素总和
        for(int left = 0,right = 0;right < n;right++) {
            tmp += cardPoints[right];
            if(right-left+1 == len) {
                sum = Math.min(sum,tmp);
                tmp -= cardPoints[left++];
            }
        }
        return ret-sum;
    }

    public String longestCommonPrefix(String[] strs) {
        int n = strs.length;
        String ret = strs[0];
        for(int i = 1;i < n;i++) {
            if(ret.equals("")) return "";  //这里要判断ret是否为空，不然下面的charAt可能越界
            String str = strs[i];
            int j  = 0;
            for(;j < ret.length() && j < str.length();j++) {
                if(ret.charAt(j) != str.charAt(j)) break;  //注意此时j指向的字符是ret和str不同的字符，但由于subString是左闭右开，所以不用修正j
            }
            ret = ret.substring(0,j);
        }
        return ret;
    }
    ////////////////////////////////////////////////////
    public double myPow(double x, int n) {
        return n > 0 ? pow(x,n) : 1 / pow(x,-n);
    }

    public double pow(double x,int n) {
        if(n == 0) return 1.0;
        double tmp = pow(x,n/2);
        return n % 2 == 0 ? tmp * tmp : tmp * tmp * x;
    }



    public static void main(String[] args) {

    }

}
